//
//  CYMoneyUnitSeg.m
//  CaiYun
//
//  Created by 今日信息科技 on 2017/8/8.
//  Copyright © 2017年 今日信息科技. All rights reserved.
//

#import "CYMoneyUnitSeg.h"
@interface CYMoneyUnitSeg(){
    Segblock _myblock;
    
}
@property (nonatomic, strong) CYButton *yuanBtn;//元
@property (nonatomic, strong) CYButton *jiaoBtn;//角
@property (nonatomic, strong) CYButton *fenBtn;//分
@property (nonatomic, strong) CYView *leftLine;
@property (nonatomic, strong) CYView *rightLine;
@end
@implementation CYMoneyUnitSeg
- (instancetype)initWithColor:(UIColor *)color size:(CGSize)size block:(Segblock)block{
    self = [super initWithColor:color];
    if (self) {
        _myblock = block;
        self.layer.cornerRadius = AL(PT(10));
        self.layer.masksToBounds = YES;
        self.layer.borderColor = ColorHex(0xb3b2b8).CGColor;
        self.layer.borderWidth = 0.5;
        self.bounds = CGRectMake(0.0, 0.0, size.width, size.height);
        self.yuanBtn.hidden = self.jiaoBtn.hidden = self.fenBtn.hidden = self.leftLine.hidden = self.rightLine.hidden = NO;
        [self performSelector:@selector(actionForYuanBtn) withObject:_yuanBtn];
    }
    return self;
}

- (CYButton *)yuanBtn{
    if (!_yuanBtn) {
        _yuanBtn = [[CYButton alloc]initWithTitle:@"元" titleColor:ColorHex(0x888888) backgroundColor:RGBA(246, 247, 248, 1) cornerRadius:0.0];
        [_yuanBtn addTarget:self action:@selector(actionForYuanBtn) forControlEvents:UIControlEventTouchUpInside];
        [_yuanBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        [_yuanBtn setBackgroundColor:ColorHex(0xff5b05) forstate:UIControlStateSelected];
        _yuanBtn.titleLabel.font = [UIFont systemFontOfSize:AL(PT(26))];
        [self addSubview:_yuanBtn];
        
        [_yuanBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_offset(UIEdgeInsetsMake(0.0, 0.0, 0.0, self.bounds.size.width / 3.0 * 2.0));
        }];
    }
    
    return _yuanBtn;
}

- (CYButton *)jiaoBtn{
    if (!_jiaoBtn) {
        _jiaoBtn = [[CYButton alloc]initWithTitle:@"角" titleColor:TEXT_GRAY_COLOR backgroundColor:RGBA(246, 247, 248, 1) cornerRadius:0.0];
        [_jiaoBtn addTarget:self action:@selector(actionForJiaoBtn) forControlEvents:UIControlEventTouchUpInside];
        [_jiaoBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        [_jiaoBtn setBackgroundColor:ColorHex(0xff5b05) forstate:UIControlStateSelected];
        _jiaoBtn.titleLabel.font = [UIFont systemFontOfSize:AL(PT(26))];
        [self addSubview:_jiaoBtn];
        [self forbidJiaoAndFen:_jiaoBtn];
        
        [_jiaoBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_offset(UIEdgeInsetsMake(0.0, self.bounds.size.width / 3.0, 0.0, self.bounds.size.width / 3.0));
        }];
    }
    
    return _jiaoBtn;
}

- (CYButton *)fenBtn{
    if (!_fenBtn) {
        _fenBtn = [[CYButton alloc]initWithTitle:@"分" titleColor:TEXT_GRAY_COLOR backgroundColor:RGBA(246, 247, 248, 1) cornerRadius:0.0];
        [_fenBtn addTarget:self action:@selector(actionForFenBtn) forControlEvents:UIControlEventTouchUpInside];
        [_fenBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        [_fenBtn setBackgroundColor:ColorHex(0xff5b05) forstate:UIControlStateSelected];\
        _fenBtn.titleLabel.font = [UIFont systemFontOfSize:AL(PT(26))];
        [self addSubview:_fenBtn];
        [self forbidJiaoAndFen:_fenBtn];
        
        [_fenBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_offset(UIEdgeInsetsMake(0.0, self.bounds.size.width / 3.0 * 2.0, 0.0, 0.0));
        }];
    }
    
    return _fenBtn;
}

- (CYView *)leftLine{
    if (!_leftLine) {
        _leftLine = [[CYView alloc]initWithColor:ColorHex(0xb3b2b8)];
        [self addSubview:_leftLine];
        
        [_leftLine mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self);
            make.left.equalTo(_yuanBtn.mas_right).with.offset(0.25);
            make.size.mas_equalTo(CGSizeMake(0.5, self.bounds.size.height - AL(PT(10)) * 2));
        }];
    }
    
    return _leftLine;
}

- (CYView *)rightLine{
    if (!_rightLine) {
        _rightLine = [[CYView alloc]initWithColor:ColorHex(0xb3b2b8)];
        [self addSubview:_rightLine];
        
        [_rightLine mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self);
            make.left.equalTo(_jiaoBtn.mas_right).with.offset(0.25);
            make.size.mas_equalTo(CGSizeMake(0.5, self.bounds.size.height - AL(PT(10)) * 2));
        }];
    }
    
    return _rightLine;
}

//元
- (void)actionForYuanBtn{
    _leftLine.hidden = YES;
    _rightLine.hidden = NO;
    _yuanBtn.selected = YES;
    _jiaoBtn.selected = _fenBtn.selected = NO;
    _myblock(@"元");
    
}

//角
- (void)actionForJiaoBtn{
    _rightLine.hidden = _leftLine.hidden = YES;
    _jiaoBtn.selected = YES;
    _yuanBtn.selected = _fenBtn.selected = NO;
    _myblock(@"角");
    
}

//分
- (void)actionForFenBtn{
    _leftLine.hidden = NO;
    _rightLine.hidden = YES;
    _fenBtn.selected = YES;
    _yuanBtn.selected = _jiaoBtn.selected = NO;
    _myblock(@"分");
    
}

- (void)forbidJiaoAndFen:(CYButton *)sender{
    sender.userInteractionEnabled = NO;
    CGSize size = [sender.titleLabel.text sizeWithAttributes:@{NSFontAttributeName:sender.titleLabel.font}];
    
    CAShapeLayer *jiaoShapeLayer = [CAShapeLayer layer];
    [jiaoShapeLayer setStrokeColor:TEXT_GRAY_COLOR.CGColor];
    [jiaoShapeLayer setLineWidth:0.5];
    
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointZero];
    [path addLineToPoint:CGPointMake(size.width,size.height)];
    jiaoShapeLayer.path = path.CGPath;
    
    [sender.titleLabel.layer addSublayer:jiaoShapeLayer];
    
}
/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
